'use strict';
import util from '../util/util.js';

import PaginationTpl from './pagination.html';
import RightTpl from './right.html';

import './pagination.scss';

export default function pagination(wraper, options) {
    let pageSize = options.pageSize || 10,
        $wraper = $(wraper),
        url = options.url,
        data = options.data || '',
        type = options.type || 'get',
        success = options.success,
        error = options.error,
        complete = options.complete,
        before = options.before,
        changeParam = options.changeParam,
        pageNO = 1,
        totalPage = 0,
        init = true,
        ajaxOptions = {
            url: url,
            data: data,
            type: type,
            success: function(json) {
                if(json.success) {
                    const result = json.data, total = result.total;
                    totalPage = Math.ceil(total / pageSize);
                    if(init && pageNO == 1) {
                        render(total, totalPage);                    
                        init = false;
                    }
                    changeRight(totalPage, pageNO);
                    $wraper.find(".js-totalpage").text(totalPage);
                    $wraper.find(".js-total-page").text(total);
    
                    success && success(result);
                }else{
                    util.alertMessage(json.error);
                }
            },
            error: function() {
                util.alertMessage('获取数据失败！');
                error && error();
            },
            complete: function() {
                complete && complete();
            }
        };
    if(!url) {
        console.error('缺少ajax参数');
    }

    queryList();

    $wraper.on('change', '.js-change-size', function() {
        pageSize = $(this).val();
        pageNO = 1;
        changeParam(setParam, pageNO, pageSize);
        queryList();
    })
    .on('click', '.js-go', function() {        
        pageNO = $('.js-pagenumber').val();
        if(pageNO) {
            changeParam(setParam, pageNO, pageSize);
            queryList();       
        }
    })
    .on('click', '.js-page', function() {
        const $this = $(this);
        let curPageNo = $this.data('page');
        if($this.hasClass('current') || $this.hasClass('disabled')) {
            return;
        }

        if(curPageNo == 'next') {
            pageNO += 1;
        }else if(curPageNo == 'prev') {
            pageNO -= 1;
        }else {
            pageNO = curPageNo;
        }
        if(pageNO <= 0) {
            pageNO = 1;
        }
        if(pageNO > totalPage) {
            pageNO = totalPage;
        }

        $('.js-page[data-page="' + pageNO + '"]').addClass('current').siblings().removeClass('current');
        changeParam(setParam, pageNO, pageSize);
        queryList();
    })
    .on('keyup', '.js-pagenumber', function() {
        const $this = $(this), reg = /^0|\D/g;
        let val = $this.val();
        val = val.replace(reg, '');
        if(!/^\s*$/.test(val) && val > totalPage) {
            val = totalPage;
        }
        $this.val(val);
    });

    /**
     * 查询列表
     */
    function queryList() {
        options.before && options.before();
        $.ajax(ajaxOptions);
    }

    /**
     * 渲染分页
     * @param {*} total 
     * @param {*} totalPage 
     */
    function render(total, totalPage) {
        $wraper.html(PaginationTpl({
            total: total,
            totalPage: totalPage,
        }));
    }

    /**
     * renderRight
     * @param {*} totalPage 
     * @param {*} current 
     */
    function changeRight(totalPage, current) {
        let totalArr = [];

        for(let i = 1; i <= totalPage; i ++) {
            totalArr.push(i);
        };
        $wraper.find('.right').html(RightTpl({
            totalPage,
            totalArr,
            current,            
        }))  
    }

    /**
     * 修改参数
     * @param {*} data 
     */
    function setParam(data) {
        ajaxOptions.data = data;        
    }

    /**
     * 修改url
     * @param newUrl 新url
     */
    function setUrl(newUrl = ajaxOptions.url){
        ajaxOptions.url = newUrl;
    }

    /**
     * 修改条件并且直接查询
    * @param {*} data 
     */
    function querySearch(data, url) {
        pageNO = 1;
        setParam(data);
        setUrl(url);
        queryList();
    }
    return querySearch;
}